/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fastagenerator.launcher;

import com.compomics.fastagenerator.fastagrabber.consoleformatter.Spinner;
import com.compomics.fastagenerator.fastagrabber.FastaFileGrabber;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Kenneth
 */
public class FastaGenerator {

    private static int amount = 21;
    private static String DATE_FORMAT_NOW = "HH_mm_ss";
    private static Calendar cal;
    private static SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

    public static void main(String[] args) {


        cal = Calendar.getInstance();
        String timestamp = sdf.format(cal.getTime());
        File locator = new File("fastaGen_output/locator");
        if (!locator.exists()) {
            locator.mkdirs();
            locator.delete();
        }

        String outputName = "FastaGen_Len_" + amount + "_" + timestamp + ".FASTA";
        File outputFile = new File(locator.getAbsolutePath().replace("locator", outputName));

        try {
            int i = 0;
            List<String> commandList = new ArrayList<String>();
            for (String aCmd : args) {
                commandList.add(aCmd);
            }
            for (String aCmd : commandList) {
                if (aCmd.toLowerCase().equals("-included_proteins") && commandList.size() != commandList.indexOf(aCmd)) {
                    try {
                        amount = Integer.parseInt(commandList.get(commandList.indexOf(aCmd) + 1));
                    } catch (NumberFormatException e) {
                        System.out.println("Invalid number...");
                        throw new Exception();
                    }
                }
                if (aCmd.toLowerCase().equals("-output") && commandList.size() != commandList.indexOf(aCmd)) {
                    outputName = commandList.get(commandList.indexOf(aCmd) + 1);
                    outputFile = new File(outputName);
                }
            }
            System.out.printf("\nSaving generated Fasta File at : ");
            System.out.println(outputFile.getAbsoluteFile());
            FastaFileGrabber.getInstance(outputFile).generateFastaNumber(amount);
            System.out.println("");
            System.out.println("Finished creating Fasta File !");
            Scanner fileScanner = new Scanner(System.in);
            Object lastKey = fileScanner.nextLine();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Invalid syntax : ");
            System.out.println("Please use the following structure : ");
            System.out.println("java -jar FastaGenerator.jar");
            System.out.printf("\n\t-included_proteins [x] \t\t (This parameter represents the amount of proteins to be included)");
            System.out.printf("\n\t-output [outputfile] \t\t(This parameter represents the file location)");
        }
    }
}
